@@ -12,7 +12,7 @@ gem 'weibo_2', '~> 0.1.4' # Weibo Agents |
||
12 | 12 |
gem 'hipchat', '~> 1.2.0' # HipchatAgent |
13 | 13 |
gem 'xmpp4r', '~> 0.5.6' # JabberAgent |
14 | 14 |
gem 'mqtt' # MQTTAgent |
15 |
-gem 'slack-notifier', '~> 0.5.0' # SlackAgent |
|
15 |
+gem 'slack-notifier', '~> 1.0.0' # SlackAgent |
|
16 | 16 |
|
17 | 17 |
# GoogleCalendarPublishAgent |
18 | 18 |
gem "google-api-client", require: 'google/api_client' |
@@ -355,7 +355,7 @@ GEM |
||
355 | 355 |
multi_json |
356 | 356 |
simplecov-html (~> 0.8.0) |
357 | 357 |
simplecov-html (0.8.0) |
358 |
- slack-notifier (0.5.0) |
|
358 |
+ slack-notifier (1.0.0) |
|
359 | 359 |
slop (3.6.0) |
360 | 360 |
spectrum-rails (1.3.4) |
361 | 361 |
railties (>= 3.1) |
@@ -509,7 +509,7 @@ DEPENDENCIES |
||
509 | 509 |
sass-rails (~> 4.0.0) |
510 | 510 |
select2-rails (~> 3.5.4) |
511 | 511 |
shoulda-matchers |
512 |
- slack-notifier (~> 0.5.0) |
|
512 |
+ slack-notifier (~> 1.0.0) |
|
513 | 513 |
spectrum-rails |
514 | 514 |
spring |
515 | 515 |
spring-commands-rspec |
@@ -1,6 +1,5 @@ |
||
1 | 1 |
module Agents |
2 | 2 |
class SlackAgent < Agent |
3 |
- DEFAULT_WEBHOOK = 'incoming-webhook' |
|
4 | 3 |
DEFAULT_USERNAME = 'Huginn' |
5 | 4 |
|
6 | 5 |
cannot_be_scheduled! |
@@ -10,15 +9,13 @@ module Agents |
||
10 | 9 |
|
11 | 10 |
description <<-MD |
12 | 11 |
#{'## Include `slack-notifier` in your Gemfile to use this Agent!' if dependencies_missing?} |
13 |
- The SlackAgent lets you receive events and send notifications to [slack](https://slack.com/). |
|
12 |
+ The SlackAgent lets you receive events and send notifications to [Slack](https://slack.com/). |
|
14 | 13 |
|
15 | 14 |
To get started, you will first need to setup an incoming webhook. |
16 |
- Go to, https://`your_team_name`.slack.com/services/new/incoming-webhook, |
|
15 |
+ Go to, <code>https://<em>your_team_name</em>.slack.com/services/new/incoming-webhook</code>, |
|
17 | 16 |
choose a default channel and add the integration. |
18 | 17 |
|
19 |
- Your webhook URL will look like: |
|
20 |
- |
|
21 |
- https://`your_team_name`.slack.com/services/hooks/incoming-webhook?token=`your_auth_token` |
|
18 |
+ Your webhook URL will look like: <code>https://hooks.slack.com/services/<em>random1</em>/<em>random2</em>/<em>token</em></code> |
|
22 | 19 |
|
23 | 20 |
Once the webhook has been setup it can be used to post to other channels or ping team members. |
24 | 21 |
To send a private message to team-mate, assign his username as `@username` to the channel option. |
@@ -28,18 +25,19 @@ module Agents |
||
28 | 25 |
|
29 | 26 |
def default_options |
30 | 27 |
{ |
31 |
- 'team_name' => 'your_team_name', |
|
32 |
- 'auth_token' => 'your_auth_token', |
|
28 |
+ 'webhook_url' => 'https://hooks.slack.com/services/...', |
|
33 | 29 |
'channel' => '#general', |
34 | 30 |
'username' => DEFAULT_USERNAME, |
35 | 31 |
'message' => "Hey there, It's Huginn", |
36 |
- 'webhook' => DEFAULT_WEBHOOK |
|
37 | 32 |
} |
38 | 33 |
end |
39 | 34 |
|
40 | 35 |
def validate_options |
41 |
- errors.add(:base, "auth_token is required") unless options['auth_token'].present? |
|
42 |
- errors.add(:base, "team_name is required") unless options['team_name'].present? |
|
36 |
+ unless options['webhook_url'].present? || |
|
37 |
+ (options['auth_token'].present? && options['team_name'].present?) # compatibility |
|
38 |
+ errors.add(:base, "webhook_url is required") |
|
39 |
+ end |
|
40 |
+ |
|
43 | 41 |
errors.add(:base, "channel is required") unless options['channel'].present? |
44 | 42 |
end |
45 | 43 |
|
@@ -47,8 +45,15 @@ module Agents |
||
47 | 45 |
received_event_without_error? |
48 | 46 |
end |
49 | 47 |
|
50 |
- def webhook |
|
51 |
- interpolated[:webhook].presence || DEFAULT_WEBHOOK |
|
48 |
+ def webhook_url |
|
49 |
+ case |
|
50 |
+ when url = interpolated[:webhook_url].presence |
|
51 |
+ url |
|
52 |
+ when (team = interpolated[:team_name].presence) && (token = interpolated[:auth_token]) |
|
53 |
+ webhook = interpolated[:webhook].presence || 'incoming-webhook' |
|
54 |
+ # old style webhook URL |
|
55 |
+ "https://#{Rack::Utils.escape_path(team)}.slack.com/services/hooks/#{Rack::Utils.escape_path(webhook)}?token=#{Rack::Utils.escape(token)}" |
|
56 |
+ end |
|
52 | 57 |
end |
53 | 58 |
|
54 | 59 |
def username |
@@ -56,7 +61,7 @@ module Agents |
||
56 | 61 |
end |
57 | 62 |
|
58 | 63 |
def slack_notifier |
59 |
- @slack_notifier ||= Slack::Notifier.new(interpolated[:team_name], interpolated[:auth_token], webhook, username: username) |
|
64 |
+ @slack_notifier ||= Slack::Notifier.new(webhook_url, username: username) |
|
60 | 65 |
end |
61 | 66 |
|
62 | 67 |
def receive(incoming_events) |
@@ -3,8 +3,7 @@ require 'spec_helper' |
||
3 | 3 |
describe Agents::SlackAgent do |
4 | 4 |
before(:each) do |
5 | 5 |
@valid_params = { |
6 |
- 'auth_token' => 'token', |
|
7 |
- 'team_name' => 'testteam', |
|
6 |
+ 'webhook_url' => 'https://hooks.slack.com/services/random1/random2/token', |
|
8 | 7 |
'channel' => '#random', |
9 | 8 |
'username' => "{{username}}", |
10 | 9 |
'message' => "{{message}}" |
@@ -25,8 +24,8 @@ describe Agents::SlackAgent do |
||
25 | 24 |
expect(@checker).to be_valid |
26 | 25 |
end |
27 | 26 |
|
28 |
- it "should require a auth_token" do |
|
29 |
- @checker.options['auth_token'] = nil |
|
27 |
+ it "should require a webhook_url" do |
|
28 |
+ @checker.options['webhook_url'] = nil |
|
30 | 29 |
expect(@checker).not_to be_valid |
31 | 30 |
end |
32 | 31 |
|
@@ -34,12 +33,8 @@ describe Agents::SlackAgent do |
||
34 | 33 |
@checker.options['channel'] = nil |
35 | 34 |
expect(@checker).not_to be_valid |
36 | 35 |
end |
37 |
- |
|
38 |
- it "should require a team_name" do |
|
39 |
- @checker.options['team_name'] = 'nil' |
|
40 |
- expect(@checker).to be_valid |
|
41 |
- end |
|
42 | 36 |
end |
37 |
+ |
|
43 | 38 |
describe "#receive" do |
44 | 39 |
it "receive an event without errors" do |
45 | 40 |
any_instance_of(Slack::Notifier) do |obj| |